
package org.sopt.dev.ImgKnight.monitor.chart;

import org.jfree.chart.JFreeChart;
import org.sopt.dev.ImgKnight.monitor.Monitor;


public class FrontierChartSet {

	private Monitor monitor = null;
	
	private MemoryMeter memoryMeter = null;
	
	private PushedUrlMeter pushedUrlMeter = null;
	
	private DroppedUrlMeter droppedUrlMeter = null;
	
	private SiteMapMultiLevelMeter siteMapMultiLevelMeter = null;
	
	private FpDocMeter fpDocMeter = null;
	
	private TraficMeter traficMeter = null;
	
	public FrontierChartSet(Monitor monitor) {
		this.monitor = monitor;
		memoryMeter = new MemoryMeter(120000);
		pushedUrlMeter = new PushedUrlMeter(120000);
		droppedUrlMeter = new DroppedUrlMeter();
		siteMapMultiLevelMeter = new SiteMapMultiLevelMeter(120000);
		fpDocMeter = new FpDocMeter(120000);
		traficMeter = new TraficMeter(120000);
	}

	public JFreeChart createMemoryLineChart() {
		return memoryMeter.createChart();
	}
	
	public JFreeChart createPushedUrlPieChart() {
		return pushedUrlMeter.createPieChart();
	}

	public JFreeChart createPushedUrlLineChart() {
		return pushedUrlMeter.createLineChart();
	}

	public JFreeChart createDroppedUrlPieChart() {
		return droppedUrlMeter.createChart();
	}
	
	public JFreeChart createSiteMapMultiLevelLineChart() {
		return siteMapMultiLevelMeter.createChart();
	}
	
	public JFreeChart createFpDocLineChart() {
		return fpDocMeter.createChart();
	}
	
	public JFreeChart createTraficLineChart() {
		return traficMeter.createChart();
	}
	
	public void updateMemotyStatus(final double totalMemory, final double freeMemory) {
		 monitor.getDisplay().asyncExec(new Runnable() {
			public void run() {
				memoryMeter.addTotalObservation(totalMemory);
				memoryMeter.addUsedObservation(totalMemory-freeMemory);
			}
		});
	}
	
	public void updatePushedUrlStatus(final double highPrioMemPushedCount, final double memPushedCount,
			final double cachePushedCount, final double diskPushedCount, final double popedCount) {
		 monitor.getDisplay().asyncExec(new Runnable() {
			public void run() {
				pushedUrlMeter.addTotalPushObservation(highPrioMemPushedCount+memPushedCount+
						cachePushedCount+diskPushedCount+popedCount);
				pushedUrlMeter.addHighPrioMemPushObservation(highPrioMemPushedCount);
				pushedUrlMeter.addMemPushObservation(memPushedCount);
				pushedUrlMeter.addCachePushObservation(cachePushedCount);
				pushedUrlMeter.addDiskPushObservation(diskPushedCount);
				pushedUrlMeter.addPopObservation(popedCount);
				pushedUrlMeter.setPieChartData("HighPrio", highPrioMemPushedCount);
				pushedUrlMeter.setPieChartData("Memory", memPushedCount);
				pushedUrlMeter.setPieChartData("Cache", cachePushedCount);
				pushedUrlMeter.setPieChartData("Disk", diskPushedCount);
				pushedUrlMeter.setPieChartData("Pop", popedCount);
			}
		});
	}
	
	public void updateDropStatus(final double duplicatedCount, final double discardCount) {
		 monitor.getDisplay().asyncExec(new Runnable() {
			public void run() {
				droppedUrlMeter.setDuplicateValue(duplicatedCount);
				droppedUrlMeter.setDiscardValue(discardCount);
			}
		});
	}
	
	public void updateSiteMapMultiLevelStatus(final double highPrioSiteMapMultiLevel, 
			final double siteMapMultiLevel) {
		 monitor.getDisplay().asyncExec(new Runnable() {
			public void run() {
				siteMapMultiLevelMeter.addHighPrioObservation(highPrioSiteMapMultiLevel);
				siteMapMultiLevelMeter.addSiteMapObservation(siteMapMultiLevel);
			}
		});
	}
	
	public void updateFpDocStatus(final double fpDocCurrSize, final double fpDocFlusedSize) {
		 monitor.getDisplay().asyncExec(new Runnable() {
			public void run() {
				fpDocMeter.addCurrentObservation(fpDocCurrSize);
				fpDocMeter.addFlushedObservation(fpDocFlusedSize);
			}
		});
	}
	
	public void updateTraficStatus(final double trafic) {
		 monitor.getDisplay().asyncExec(new Runnable() {
			public void run() {
				traficMeter.addTraficObservation(trafic);
			}
		});
	}
}
